Skip to content

Change SumoBot to deleted_user #6537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions kitsune/messages/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ def on_user_deletion(self, user: User) -> None:
When a user is deleted:
- Delete their outbox messages
- Keep inbox messages for other users and reassign them to SumoBot
- Update outbox message recipients to SumoBot where the deleted user was a recipient
"""
sumo_bot = Profile.get_sumo_bot()
InboxMessage.objects.filter(to=user).delete()
InboxMessage.objects.filter(sender=user).update(sender=sumo_bot)

# Update outbox messages where the deleted user was a recipient
outbox_messages = OutboxMessage.objects.filter(to=user)
for message in outbox_messages:
message.to.remove(user)
message.to.add(sumo_bot)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of assigning these messages to SuMoBot? Good clean up work on removing the user from the recipient list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If userA sent a message to userB and userB gets deleted, the message in userA's Outbox needs to have a 'To' person - thus the reassignment.


OutboxMessage.objects.filter(sender=user).delete()
4 changes: 2 additions & 2 deletions kitsune/messages/jinja2/messages/inbox.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "messages/base.html" %}
{% from "messages/includes/macros.html" import avatar_link, name_link %}
{% from "messages/includes/macros.html" import avatar_link, display_user %}
{% set title = _('Inbox') %}
{% set crumbs = [(url('messages.inbox'), _('Messages')),
(None, title)] %}
Expand Down Expand Up @@ -37,7 +37,7 @@ <h1 class="sumo-page-heading">{{ title }}</h1>
{{ avatar_link(message.sender, default_avatar) }}
</div>
<div class="email-cell from">
{{ name_link(message.sender) }}
{{ display_user(message.sender) }}
</div>
<div class="email-cell date">
{{ datetimeformat(message.created) }}
Expand Down
16 changes: 12 additions & 4 deletions kitsune/messages/jinja2/messages/includes/macros.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{% from "includes/common_macros.html" import content_editor with context %}

{% macro display_user(user) -%}
{% if user.profile.is_system_account %}
{{ _('deleted user') }}
{% else %}
{{ name_link(user) }}
{% endif %}
{%- endmacro %}

{% macro avatar_link(user=None, default_avatar=None) -%}
{% if user %}
<a rel="nofollow" href="{{ profile_url(user) }}">
Expand Down Expand Up @@ -42,7 +50,7 @@
{{ avatar_link(message.sender, default_avatar) }}
</div>
<div class="user from">
{{ name_link(message.sender) }} {{ datetimeformat(message.created) }}
{{ display_user(message.sender) }} {{ datetimeformat(message.created) }}
</div>
</div>
</section>
Expand All @@ -55,11 +63,11 @@
{% if message.recipients_count > 0 %}
<p><strong>{{ _('To') }}:</strong>
{% set comma = joiner(', ') %}
{% for user in message.to.all() -%}
{% for user in message.to_users -%}
{{ comma() }}
{{ name_link(user) }}
{{ display_user(user) }}
{% else %}
{{ name_link(message.recipient) }}
{{ display_user(message.recipient) }}
{% endfor %}
</p>
{% endif %}
Expand Down
6 changes: 2 additions & 4 deletions kitsune/messages/jinja2/messages/outbox.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "messages/base.html" %}
{% from "messages/includes/macros.html" import avatar_link, name_link %}
{% from "messages/includes/macros.html" import avatar_link, display_user %}
{% set title = _('Sent Messages') %}
{% set crumbs = [(url('messages.inbox'), _('Messages')),
(None, title)] %}
Expand Down Expand Up @@ -38,9 +38,7 @@ <h1 class="sumo-page-heading">{{ title }}</h1>
<div class="email-cell sent">{{ datetimeformat(message.created) }}</div>
<div class="email-cell to">
{% for user in message.to.all()[:1] -%}
<a rel="nofollow" href="{{ profile_url(user) }}">
{{ user.profile.display_name }}
</a>
{{ display_user(user) }}
{%- if message.recipients_count > 1 -%}, ...{% endif %}
{%- endfor %}
</div>
Expand Down
29 changes: 27 additions & 2 deletions kitsune/messages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def inbox(request):
.prefetch_related("sender__profile")
)
count = messages.count()

messages = paginate(request, messages, per_page=MESSAGES_PER_PAGE, count=count)

return render(
Expand All @@ -42,19 +41,25 @@ def read(request, msgid):
was_new = message.unread
if was_new:
message.update(read=True)

initial = {"to": message.sender, "in_reply_to": message.pk}
form = ReplyForm(initial=initial)
response = render(
request,
"messages/read.html",
{"message": message, "form": form, "default_avatar": settings.DEFAULT_AVATAR},
{
"message": message,
"form": form,
"default_avatar": settings.DEFAULT_AVATAR,
},
)
return response


@login_required
def read_outbox(request, msgid):
message = get_object_or_404(OutboxMessage, pk=msgid, sender=request.user)

return render(
request,
"messages/read-outbox.html",
Expand Down Expand Up @@ -179,6 +184,26 @@ def preview_async(request):


def _add_recipients(msg):
"""Process and attach recipient information to a message object.
This helper function calculates recipient counts and attaches recipient-related
attributes to the message object for display purposes.
Args:
msg: An OutboxMessage object to process.
Returns:
The modified message object with the following attributes set:
- recipients_count: Total number of individual recipients
- to_groups_count: Total number of group recipients
- recipient: The first recipient if there is exactly one
individual recipient, else None
- to_groups: List of recipient groups with prefetched profiles
Note:
The function expects msg.to and msg.to_group to be prefetched related fields
on the OutboxMessage object.
"""
# Set the counts based on the lists
msg.recipients_count = msg.to.all().count()
msg.to_groups_count = msg.to_group.all().count()
Expand Down